package com.service;

import com.dao.DishMapper;
import com.pojo.Dish;
import com.pojo.DishExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DishService {
    @Autowired
    DishMapper dishMapper;

    //根据菜名查询
    public Dish getDishByName (String dName) {
        DishExample example = new DishExample();
        DishExample.Criteria criteria = example.createCriteria();
        criteria.andDNameEqualTo(dName);
        if (dishMapper.selectByExampleWithType(example) == null)
            return null;
        else
            return dishMapper.selectByExampleWithType(example).get(0);
    }

    public List<Dish> getAll () {
        return dishMapper.selectByExampleWithType(null);
    }

    //根据菜品类型查询
    public List<Dish> getBydtId (Integer value) {
        DishExample example = new DishExample();
        DishExample.Criteria criteria = example.createCriteria();
        criteria.andDtIdEqualTo(value);
        return dishMapper.selectByExample(example);
    }

    public void saveDish (Dish dish) {
        dishMapper.insertSelective(dish);
    }

    public boolean checkDish (String dName) {
        DishExample example = new DishExample();
        DishExample.Criteria criteria = example.createCriteria();
        criteria.andDNameEqualTo(dName);
        long count = dishMapper.countByExample(example);
        return count == 0;
    }

    public void deleteDish (Integer id) {
        dishMapper.deleteByPrimaryKey(id);
    }

    public void updateDish (Dish dish) {
        dishMapper.updateByPrimaryKeySelective(dish);
    }

    public Dish getDishByDId(Integer dId){
        return dishMapper.selectByPrimaryKey(dId);
    }

}
